<?php
include_once 'CF_Table.php';
/**
 * CFtable is a class to simplfy creation of Databse CRUD (Create Remove Update Delete) Tables
* @package CFtable
 * @author Chris Tyler
 * @version 1.0
 * @CFtable is the base class for the an extensable HTML table class for array based data.  This provides data display, sorting, filtering, paging and heading functions
 * @copyright 2007-2008 
 */

class CF_Table extends CF_TableBasic{
	/**
	* @var string $table_name Defines name of the table (associated elements)
	*/	
	var $table_name = 'datatable';
	/**
	* @var string $table_text String of HTML for output as table
	*/	
	var $table_text;
	/**
	* @var string $selfpage The name of the page, usualy set by $_SERVER['SELF']
	*/
	var $selfpage;
	/**
	* @var array $gotopages The array of pages identified by the action, for the system to link to after ahe action
	* example:  $gotopages['edit'] = someformpage.php?mode=save
	*/
	var $gotopages = array();	
	/**
	* @var string $referstring :  A string to tag along to any pages that are called.  not tested
	*/
	var $referstring;	
	/**
	* @var string $liststartvar String used for list start variable
	*/	
	var $liststartvar = 'cfListStart'; 
	/**
	* @var array ar_lookups:  array of arrays (named by $column) of lookup arrays.  
	* Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	* where n starts at 0
	*/	
	var $ar_lookups = array();
	// make an index by keyvalue of ar_lookups
	var $ar_index_lookups = array();
	
	var $listsizevar = 'cfListSize';
	/**
	* @var string $top_dir Set absolute or relative reference to the base directory (for things in subdirectories)
	*/		
 	var $top_dir = '';

	/**
	* @var string $editvarprefix prefix for filtered variables (column name used)
	*/
	var $editvarprefix = '';


	// ----------------------------------- Variables changed by functions (might make these private someday...)
	
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/	
    var $keys_url_edit;
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/
    var $keys_url_act;

	/**
	* @var string $keys_uid keys as semicolon (;) delimited fields per row
	*/    
	var $keys_uid;
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	

	

// ---------------------------------FUNCTIONS---------------------------------
// ---------------------------------Public Functions--------------------------
	
/**
 * Constructor.  Override / extend
 * @param array $thespec - The fieldspec array
 * @param array $dataarray - The data array, data[<rownumber>][<column_name>]
 */		
	function __construct( $thespec = '', $dataarray= '', $name = ''){
		$this->fieldspec = $thespec;	
		$this->valuesarray = $dataarray;
		
		$this->selfpage = $_SERVER['PHP_SELF'];

		$this->name = $name;
	}
	
	function CF_Table( $thespec = '', $dataarray= '', $name = '' ){
		__construct($thespec,$dataarray,$name);
	}
	
	
	/**
	 * setLookups(array $lookups) : This sets the lookup arrays to an array of arrays for each column
	 * @param array $lookups:  The array of arrays, by column.  Each array has form
     * Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	 * where n starts at 0
	 *
	 */
	function setLookups($lookups){
		foreach($lookups AS $column=>$lookup){
			$this->setLookup($column,$lookup);
		}
	}
	
	/**
	 * setLookup($column,$lookup)
	 * @param string $column:  The name of the column for the lookup
	 * @param array $lookup:  The lookup array
	 * Each lookup array has form $lookup[<n>]['keyvalue'] = <key>, $lookup['showvalue']=<thing to show>
	 * where n starts at 0
	 *
	 */	 
	function setLookup($column,$lookup){
		foreach($lookup AS $row=>$pair){
			$this->ar_index_lookups[$column][$pair['keyvalue']] = $pair['showvalue'];
		}
		$this->ar_lookups['column']  = $lookup;
	}
	function set_lookup($column,$lookup){
		$this->setLookup($column,$lookup);
	}
	
	
   	/**
	 *  @param string $name Table Name
	 */	
	function set_name($name){
		$this->table_name = $name;	
	}
   	/**
	 * set_ordervar The variable to use for specifying column to order by  
	 * @param string $newordervar
	 */		
	function set_ordervar($newordervar){
		$this->ordervar = $newordervar;
	}	
	
	/**
	 * set_selfpage Set the page, usually set to $_SERVER['PHP_SELF']
	 * @param string $newpage The page 
	 */	
	function set_selfpage($newpage){
		$this->selfpage = $newpage;
	}	

	// Also in CF_Multiselect
   	/**
	 *  make_keyvariables  Method will output a comma delimited list of variables (columns) which are keys from fieldspec
	 */
	function make_keyvariables(){
		$aftercomma = '';
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				$keyvariables .= $aftercomma . $col_name;
				$aftercomma = ',';
			}
		}
		return $keyvariables;
	}
	

/**
 * function to make start of row.  Override / extend
 * @param int $row_num This is the row number of the dataset
 * @param string $key_string This is the string of key values, defined by make_key_string function
 * @return string Row begining 
 */
	function make_row_start($row_num){
		$rowstart = '';
		if($this->row_status == 'even'){
			$this->row_status = 'odd';
			$rowstart .= "<tr class=\"$this->table_even_class\">";			
		}
		else{
			$this->row_status = 'even';
			$rowstart .= "<tr class=\"$this->table_odd_class\">";			
		}
		//if($this->multiselect){
		//	$rowstart .= "<td>" . $this->make_multiselectCB() . "</td>";
		//}	
		return $rowstart;
	}

	function make_multiselectCB(){
		return "<input type='checkbox' name='" . $this->table_name . "_addto_ms' value='" . $this->keys_uid . "' >";
	
	}	
	
	function make_row_end($row_num){
		return "</tr>\n";
	}
	
	function make_rowval($value,$column){
		$row_text = '';
		$row_text .= "<td>";
		// substitute if type = dropdown (D) and looukp array is set
		if(($this->fieldspec[$column]['select'] == 'D') AND (is_array($this->ar_index_lookups[$column]))){
			$row_text .= $this->ar_index_lookups[$column][$value];
		}else{
			$row_text .= $value;
		}
		$row_text .= "</td>";
		return $row_text;
			
	}

	function clear_keys_strings() { 
		$this->keys_url_edit = '';
        $this->keys_url_act = '';
		$this->keys_uid = '';
	}
	

	/**
	 *(Also in CF_Filter and CF_AddBox)
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }

	
	/**
	 *
     * @make_form_set set will create a form out of the elements passed in
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */	
    function make_form_set($elements, $headset, $bodyset){

        if(is_array($elements)){
			foreach ($elements AS $col_name=>$current){
				$formtext .= "<div class='ms_$col_name' ><label for='$col_name'>" . $current['head'] . "</label>\n" . $current['body'] . "</div>\n";
			}
        }
        return "<fieldset>\n$formtext</fieldset>\n";
    }	
	
	/**
	 *
     * @make_element_div_set set will create a set of div for use as a form
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_div_set($elements, $headset, $bodyset){
        foreach ($elements AS $name=>$current){
            $outtext .= "<div id='entry_$name' class='entries'>\n";
            foreach($current AS $key=>$value){
            
                $outtext .= "<div class='$key'>\n $value </div>\n";
                }
            $outtext .= "</div>\n";
         }
         
        return $outtext;
    }
	



	function get_edit_keys(){
	//for each column which is a key, get the key and set the keys variable array
	foreach ($this->fieldspec as $col_name => $colspec){
		if((!empty($colspec['key'])) AND (isset($_REQUEST[$col_name])) AND ($_REQUEST[$col_name] != '') ){
			$this->keys[$col_name] = $_REQUEST[$col_name];
			}
		}
		//print_r ($this->keys);
	}
	
	
	/**
	 * function to make key string.  Override / extend
	 * @param int $col_name This is the column name for the key value
	 * @param string $currentvalue This is the value of the key
	 * @param int $firstrow This is 1 for the first row, 0 afterwards for special punctuation
	 * @return string keystring
	 */	
    function make_key_string($col_name, $currentvalue, $firstrow = 0){
		if(empty($this->keys_uid)){
			$this->keys_uid =  "$currentvalue";
	
		}
		else{
			$this->keys_uid .=  ",$currentvalue";
		}
		$this->keys_hidden .= "<input type='hidden' name='$col_name' value='$currentvalue'>";
		//parent::make_key_string($col_name, $currentvalue);
		$this->keys_url_edit .= "&$this->editvarprefix" . "$col_name=$currentvalue";
        $this->keys_url_act .= "&$col_name=$currentvalue";
		
	}
	
	function make_keys_clause(){
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				if(isset($this->keys[$col_name])){ // check if defined and exists in keys array
					$outtext .= " AND $col_name = '" . $this->keys[$col_name] . "' ";
				}else{
					$outtext .= " AND 0 "; // Not all specified keys were defined, so return 0 to kill any results form query
				}
			}
		}
		return $outtext ;
	}
	
	function make_keys_hidden(){
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($colspec['key'] )){
				if(isset($this->keys[$col_name])){ // check if defined and exists in keys array
					$outtext .= " <input type='hidden' name='$col_name' value='" . $this->keys[$col_name] . "'> ";
				}
			}
		}
		return $outtext;
	}
		
	
}


?>